package com.luxury.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luxury.R;
import com.luxury.adpter.Tablayou_ViewPagerAdapter;
import com.luxury.fragment.party_fragment.GlobalFragment;
import com.luxury.fragment.party_fragment.KuDaShiFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/12/5.
 */

public class PartyFragment extends Fragment {
    @BindView(R.id.tablayout_fragment_party)
    TabLayout tablayout_Party;
    @BindView(R.id.viewPager_fragment_party)
    ViewPager viewPager_Party;
    private Context mContext;
    private Tablayou_ViewPagerAdapter mAdapter;
    private List<Fragment> mList = new ArrayList<>();
    private String[] mArrTabTitles_party = null;

    public static PartyFragment newInstance() {
        Bundle args = new Bundle();
        PartyFragment fragment = new PartyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_party, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initFragment() {
        //        得到title的数组(此处应该是json解析出来的title 我写死了 日后再说)
        mArrTabTitles_party = getResources().getStringArray(R.array.tabtitle_party);
        //        将fragment添加到集合中
        GlobalFragment golbal_fragment = GlobalFragment.newInstance();
        KuDaShiFragment kudashi_fragment = KuDaShiFragment.newInstance();
        mList.add(golbal_fragment);
        mList.add(kudashi_fragment);
    }

    private void initData() {
        mAdapter = new Tablayou_ViewPagerAdapter(getActivity().getSupportFragmentManager(), mList,
                mArrTabTitles_party);
        viewPager_Party.setOffscreenPageLimit(3);
        viewPager_Party.setAdapter(mAdapter);
        tablayout_Party.setupWithViewPager(viewPager_Party);
    }
}
